﻿Imports Actuariat.comptes
Imports Actuariat

Public Class vueBilan_V2
    Inherits vueCompte_V2

    Public Overrides WriteOnly Property moteurTxGaranti2() As moteurTauxGaranti2.Moteur_Methode2
        Set(ByVal value As moteurTauxGaranti2.Moteur_Methode2)
            MyBase.moteurTxGaranti2 = value
            AddHandler _resultatsScenarioCentral.compteBilan_hasChanged, AddressOf onResultatsScenCent_CompteBilan_HasChanged
            AddHandler _resultatsScenarioCentral.compteBesoinsFdsPropres_hasChanged, AddressOf onResultatsScenCent_CompteBesoinsFdsPropres_HasChanged
        End Set
    End Property

#Region "Initialisation"
    Protected Overrides Sub initNbLignes()
        If Me.ColumnCount = 0 Then
            Me.ColumnCount = 1
        End If
        For i As Integer = 1 To 32
            Me.Rows.Add()
            Me.Rows(i - 1).Height = 15
        Next
        'Cas particuliers
        Rows(3).Height = 6
        Rows(9).Height = 6
        Rows(23).Height = 6
    End Sub
    Protected Overrides Sub initTitresBlocs()
        initLignesBlocActif()
        initLignesBlocPassif()
        initTitreFdsPropres()
        initLignesBlocCapiAlloues()
    End Sub
    Protected Overrides Sub initLignesTitresGeneraux()
        Me.Rows(0).Cells(0).Value = "BILAN"
    End Sub
    Protected Sub initTitreFdsPropres()
        'Titre des besoins en fonds propres
        Me.Rows(20).Cells(0).Value = "LE BESOIN EN FONDS PROPRES"
    End Sub
    Protected Sub initLignesBlocActif()
        'Titre du bloc
        Me.Rows(2).Cells(0).Value = "ACTIF"
        'Intitulés des comptes
        Me.Rows(4).Cells(0).Value = "Capital social non libéré"
        Me.Rows(5).Cells(0).Value = "Placements"
        Me.Rows(6).Cells(0).Value = "Total"
        'Réglage de la police
        Me.Rows(4).Cells(0).Style.Font = New System.Drawing.Font("Tahoma", 9, FontStyle.Underline)
        Me.Rows(5).Cells(0).Style.Font = New System.Drawing.Font("Tahoma", 9, FontStyle.Underline)
    End Sub
    Protected Sub initLignesBlocPassif()
        'Titre
        Me.Rows(8).Cells(0).Value = "PASSIF"
        'Intitulés des comptes
        Me.Rows(10).Cells(0).Value = "Capitaux propres"
        Me.Rows(11).Cells(0).Value = "   Capital social ouverture"
        Me.Rows(12).Cells(0).Value = "   Apport/Retraits de capitaux (solv)"
        Me.Rows(13).Cells(0).Value = "   Résultat de l'exercice N-1"
        Me.Rows(14).Cells(0).Value = "   Apport/Retraits de capitaux (res Neg)"
        Me.Rows(15).Cells(0).Value = "Passifs subordonnés"
        Me.Rows(16).Cells(0).Value = "Provisions techniques brutes"
        Me.Rows(17).Cells(0).Value = "Total"
        'Réglage de la police d'écriture
        Me.Rows(10).Cells(0).Style.Font = New System.Drawing.Font("Tahoma", 9, FontStyle.Underline)
        Me.Rows(15).Cells(0).Style.Font = New System.Drawing.Font("Tahoma", 9, FontStyle.Underline)
        Me.Rows(16).Cells(0).Style.Font = New System.Drawing.Font("Tahoma", 9, FontStyle.Underline)
    End Sub
    Protected Sub initLignesBlocCapiAlloues()

        'Titre du compte de résultat
        Me.Rows(22).Cells(0).Value = "CAPITAUX ALLOUES"
        Me.Rows(22).Cells(0).Style = styleCellulesTitre2()
        'Intitulé des comptes
        Me.Rows(24).Cells(0).Value = "MSR"
        Me.Rows(25).Cells(0).Value = "   MSR (vie)"
        Me.Rows(26).Cells(0).Value = "   MSR (non vie)"
        Me.Rows(27).Cells(0).Value = "Capitaux alloués"
        Me.Rows(28).Cells(0).Value = "   Capitaux alloués Vie"
        Me.Rows(29).Cells(0).Value = "   Capitaux alloués Non Vie"
        Me.Rows(30).Cells(0).Value = "Capitaux alloués"

        Me.Rows(24).Cells(0).Style.Font = New System.Drawing.Font("Tahoma", 9, FontStyle.Underline)
        Me.Rows(27).Cells(0).Style.Font = New System.Drawing.Font("Tahoma", 9, FontStyle.Underline)
    End Sub

#End Region

#Region "Dessin"
    Protected Overrides Sub paintBlocs(ByVal e As System.Windows.Forms.DataGridViewCellPaintingEventArgs)
        'Le bloc "Actif"
        paintBloc(e, 2, 6)
        'Le bloc "Passif"
        paintBloc(e, 8, 17)
        'Le bloc des capitaux alloués
        paintBloc(e, 22, 30)
        'Colonne de titre des besoins en fonds propres
        paintLigneTitre(e, 20)
    End Sub
    Protected Overrides Function cellFormat(ByVal rowIndex As String, ByVal columnIndex As String) As String
        If rowIndex <> 0 And columnIndex <> 0 Then Return _formatDefaut Else Return ""
    End Function
    Protected Overrides Function cellBackColor(ByVal rowIndex As String, _
                                   ByVal columnIndex As String, _
                                   ByVal ligneHautBloc As Integer, _
                                   ByVal ligneBasBloc As Integer) As System.Drawing.Color
        If rowIndex = ligneHautBloc Then
            Return Color.FromArgb(231, 213, 231)
        ElseIf rowIndex = ligneBasBloc Then
            Return Color.FromArgb(255, 230, 30)
        Else
            Return Color.White
        End If
    End Function
    Protected Overrides Function cellAlignment(ByVal rowIndex As Integer, ByVal columnIndex As Integer) As StringAlignment
        If columnIndex = 0 Then Return StringAlignment.Near Else Return StringAlignment.Far
    End Function
    Protected Overrides Function cellFont(ByVal rowIndex As Integer, ByVal columnIndex As Integer, ByVal ligneHautBloc As Integer, ByVal ligneBasBloc As Integer) As Font
        If rowIndex = ligneHautBloc Or rowIndex = ligneBasBloc Then
            Return New System.Drawing.Font("Tahoma", 9, FontStyle.Bold)
        Else
            Return New System.Drawing.Font("Tahoma", 9, FontStyle.Regular)
        End If
    End Function
#End Region

#Region "Rafraichissements des chiffres des comptes"
    Private Sub onResultatsScenCent_CompteBilan_HasChanged(ByVal sender As Object, ByVal e As EventArgs)
        refreshValeursBilan()
    End Sub
    Private Sub onResultatsScenCent_CompteBesoinsFdsPropres_HasChanged(ByVal sender As Object, ByVal e As EventArgs)
        refreshValeursBesoinsFondsPropres()
    End Sub
    Protected Function refreshValeursBesoinsFondsPropres()
        If Not _resultatsScenarioCentral Is Nothing Then
            Me.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.None
            For i As Integer = 1 To _nbAnneesProjection
                With _resultatsScenarioCentral.compteBesoinsFdsPropres
                    If Not .MSRvie Is Nothing Then
                        If i <= .MSRvie.Count Then
                            Me.Rows(25).Cells(i).Value = .MSRvie(i - 1) / unite
                            Me.Rows(26).Cells(i).Value = .MSRnonVie(i - 1) / unite
                            Me.Rows(28).Cells(i).Value = .capiAllouesVie(i - 1) / unite
                            Me.Rows(29).Cells(i).Value = .capiAllouesNonVie(i - 1) / unite
                            Me.Rows(30).Cells(i).Value = .capiAlloues(i - 1) / unite
                        End If
                    End If
                End With
            Next
            Me.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells
        End If
        Return Nothing
    End Function
    Protected Function refreshValeursBilan()
        If Not _resultatsScenarioCentral Is Nothing Then
            Me.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.None
            For i As Integer = 1 To _nbAnneesProjection
                With _resultatsScenarioCentral.compteBilan
                    If Not .capitalSocialNonLibere Is Nothing Then
                        If i <= .capitalSocialNonLibere.Count Then
                            Me.Rows(4).Cells(i).Value = .capitalSocialNonLibere(i - 1) / unite
                            Me.Rows(5).Cells(i).Value = .placements(i - 1) / unite
                            Me.Rows(6).Cells(i).Value = .totalActif(i - 1) / unite
                            Me.Rows(11).Cells(i).Value = .capitalSocialOuv(i - 1) / unite
                            Me.Rows(12).Cells(i).Value = .apportSolvabilite(i - 1) / unite
                            Me.Rows(13).Cells(i).Value = .resultatN_1(i - 1) / unite
                            Me.Rows(14).Cells(i).Value = .apportResultatNeg(i - 1) / unite
                            Me.Rows(15).Cells(i).Value = .passifsSubordonnes(i - 1) / unite
                            Me.Rows(16).Cells(i).Value = .provisionsTechniquesBrutes(i - 1) / unite
                            Me.Rows(17).Cells(i).Value = .totalPassif(i - 1) / unite
                        End If
                    End If
                End With
            Next
            Me.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells
        End If
        Return Nothing
    End Function
    Protected Overrides Sub refreshValeursBlocs()
        If (Not _resultatsScenarioCentral Is Nothing) Then
            refreshValeursBesoinsFondsPropres()
            refreshValeursBilan()
        End If
    End Sub
#End Region

#Region "Rafraichissements graphiques"
    Protected Overrides Sub refreshTitresGeneraux()
        'Années de projection
        For j As Integer = 1 To _nbAnneesProjection
            Me.Rows(0).Cells(j).Value = CStr(j) - 1 + _anneeDepart
            Me.Rows(20).Cells(j).Value = CStr(j) - 1 + _anneeDepart
        Next
    End Sub
#End Region

    Protected Overrides Sub Dispose(ByVal disposing As Boolean)
        Try
            RemoveHandler _resultatsScenarioCentral.compteBilan_hasChanged, AddressOf onResultatsScenCent_CompteBilan_HasChanged
            RemoveHandler _resultatsScenarioCentral.compteBesoinsFdsPropres_hasChanged, AddressOf onResultatsScenCent_CompteBesoinsFdsPropres_HasChanged
        Finally
            MyBase.Dispose(disposing)
        End Try
    End Sub

End Class